package org.ade.monak.anak.boundary;

import org.ade.monak.anak.R;
import org.ade.monak.anak.entity.Ortu;
import org.ade.monak.anak.service.DatabaseThreadService;
import org.ade.monak.anak.service.MonakServiceAnak;
import org.ade.monak.anak.service.MonakServiceAnak.BinderService;
import org.ade.monak.anak.service.gate.monak.SenderLokasi;
import org.ade.monak.anak.service.storage.DatabaseManagerAnak;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

public class PendaftaranAnak extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.pendaftaran_anak);
		
		Bundle bundle = getIntent().getExtras();
		
		noHp 	= bundle.getString("noHp");
		idAnak 	= bundle.getString("idAnak");
		idOrtu 	= bundle.getString("idOrtu");
		
		final Handler handler = new Handler();
		
		final DatabaseManagerAnak databaseManager = new DatabaseManagerAnak(this);
		
		Button buttonTerima = (Button) findViewById(R.id.btnTerima);
		buttonTerima.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				databaseThreadService.addDatabaseThread(new Runnable(){

					public void run() {
						final Ortu ortu = new Ortu();
						ortu.setIdOrtu(idOrtu);
						ortu.setIdAnak(idAnak);
						ortu.setNoHp(noHp);	
						Ortu ortuDatabase = databaseManager.getOrtuByIdOrtu(idOrtu, false);
						ortu.setTrack(false);
						
						if(ortuDatabase!=null){
							databaseManager.deleteOrtu(ortuDatabase);
						}
								
						databaseManager.addOrtu(ortu);	
						handler.post(new Runnable(){

							public void run() {
								SenderLokasi senderLokasi = new SenderLokasi(PendaftaranAnak.this, ortu);
								senderLokasi.sendLocationSingleRequest(idAnak);
							}
							
						});
					
					}
					
				});
			
				finish();
			
			}
		});
		
		Button buttonTidak = (Button) findViewById(R.id.btnTidak);
		buttonTidak.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				finish();
			}
			
		});
	}
	
	
	
	@Override
	protected void onStart() {
		super.onStart();
		serviceConnection = new ServiceMonakConnection(this);
		bindService(new Intent(MonakServiceAnak.MONAK_SERVICE_ANAK), 
													serviceConnection, 
													Context.BIND_AUTO_CREATE);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(bound){
			unbindService(serviceConnection);
		}
		bound = false;
	}


	private boolean bound;
	private String noHp;
	private String idAnak;
	private String idOrtu;
	private DatabaseThreadService databaseThreadService;
	private ServiceMonakConnection serviceConnection;
	
	
	private class ServiceMonakConnection implements ServiceConnection{

		public ServiceMonakConnection(PendaftaranAnak pendaftaranAnak){
			this.pendaftaranAnak = pendaftaranAnak;
		}
		
		public void onServiceConnected(ComponentName name, IBinder service) {
			bound = true;
			pendaftaranAnak.databaseThreadService = 
					((BinderService) service).
						getMonakService().getDatabaseThread();
		}

		public void onServiceDisconnected(ComponentName name) {
			bound = false;
		}
		
		private final PendaftaranAnak pendaftaranAnak;
	}

	
}
